package com.atguigu.srb.sms.controller.api;

import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "短信服务")
@RestController

@RequestMapping("/api/sms")
public class ApiSmsController {
    @Resource
    private CoreUserInfoClient coreUserInfoClient;
    @Resource
    private SmsService smsService;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("获取验证码")
    @GetMapping("/send/{mobile}")
    //以后记得不要在controller层里写业务！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    public R send(@PathVariable("mobile") @ApiParam("手机号码") String mobile){

        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        boolean b = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(b,ResponseEnum.MOBILE_ERROR);



        //远程调用
        boolean bCheck = coreUserInfoClient.checkMobile(mobile);
//        if (bCheck){
//            return R.error().message("手机号已注册");
//        }

       Assert.isTrue(!bCheck,ResponseEnum.MOBILE_EXIST_ERROR);

        smsService.send(mobile);
        return R.ok();



//        String code = RandomUtils.getSixBitRandom();
//        HashMap<String, Object> param = new HashMap<>();
//        param.put("code",code);
//        redisTemplate.opsForValue().set("srb:sms:code:"+mobile,code,5, TimeUnit.MINUTES);
//
//        System.out.println(code);
//
//        smsService.send(mobile, SmsProperties.TEMPLATE_CODE,param);

    }


    @ApiOperation("测试sentinel")
    @GetMapping("/test")
    public R test(){
        String test = coreUserInfoClient.test();
        System.out.println("provider"+test);
        return R.ok().message("sentinel测试"+test);
    }

}
